﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using QuickTasks.ViewModels.Properties;
using LocalSettings;
using QuickTasks.ViewModels.Tables;
using Microsoft.Advertising.Mobile.UI;


namespace QuickTasks.Views.FavoritePeople
{
    public partial class SelectLocation : PhoneApplicationPage
    {
        string Lid;
        string type;
        public SelectLocation()
        {
            InitializeComponent();
           
            Loaded += new RoutedEventHandler(SelectLocation_Loaded);
        }
      
        void SelectLocation_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (NavigationContext.QueryString.TryGetValue("type", out type))
                {
                }
                if (NavigationContext.QueryString.TryGetValue("Id", out Lid))
                {
                    List<AddressProp1> loc = new List<AddressProp1>();
                    using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                    {
                        var addressid = from i in context.Add where i.id.ToString() != IsolatedSettings.getSettingsStringValue("DefaultPlace") orderby i.id descending select i;
                        foreach (var ele in addressid)
                        {
                            AddressProp1 objAddressProp = new AddressProp1();
                            objAddressProp.Id = ele.id.ToString();
                            objAddressProp.Address = ele.address.ToString();
                            loc.Add(objAddressProp);
                        }
                    }
                    if (loc.Count == 0)
                    {
                        AddressProp1 objAddressProp = new AddressProp1();
                        objAddressProp.Address = "no Locations avaliable";
                        loc.Add(objAddressProp);
                    }
                    lbxLocation.ItemsSource = loc;
                }
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in SelectLocation_Loaded method In SelectLocation.xaml page", ex);
            
            }
        }

        private void lbxLocation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                string listid = (lbxLocation.SelectedItem as AddressProp1).Id;
                using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                {
                    if (type == "people")
                    {
                        System.Linq.IQueryable<people> addressid = from i in context.people where i.id.ToString() == Lid select i;
                        if (addressid.Count() > 0)
                        {
                            people peo = addressid.FirstOrDefault();
                            {
                                peo.place = listid;
                            }

                            context.SubmitChanges();
                        }
                    }
                    else if (type == "message")
                    {
                        System.Linq.IQueryable<messages> addressid = from i in context.message where i.id.ToString() == Lid select i;
                        if (addressid.Count() > 0)
                        {
                            messages mes = addressid.FirstOrDefault();
                            {
                                mes.place = listid;
                            }

                            context.SubmitChanges();
                        }
                    }
                    else if (type == "link")
                    {
                        System.Linq.IQueryable<Links> addressid = from i in context.link where i.id.ToString() == Lid select i;
                        if (addressid.Count() > 0)
                        {
                            Links link = addressid.FirstOrDefault();
                            {
                                link.place = listid;
                            }

                            context.SubmitChanges();
                        }
                    }
                    else if (type == "todo")
                    {
                        System.Linq.IQueryable<Todo> addressid = from i in context.Todo where i.id.ToString() == Lid select i;
                        if (addressid.Count() > 0)
                        {
                            Todo todo = addressid.FirstOrDefault();
                            {
                                todo.place = listid;
                            }

                            context.SubmitChanges();
                        }
                        System.Linq.IQueryable<Reminders> remid = from i in context.reminderalr where i.id.ToString() == Lid select i;
                        if (remid.Count() > 0)
                        {
                            Reminders r = remid.FirstOrDefault();
                            {
                                r.place = listid;
                            }
                            context.SubmitChanges();
                        }
                    }
                    else
                    {
                    }
                }
                NavigationService.GoBack();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxLocation_SelectionChanged event In SelectLocation.xaml page", ex);
            }
        }
    }
}